<template>
  <a-modal v-model:visible="visible" fullscreen :footer="false">
    <template #title>{{ currentRow.name }} 保险配置</template>
    
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      
      <template #tableAfterButtons>
        <div class="tips">注意：如果车辆保险需要单独设置保险，就在本页单独添加，不需要单独设置的就不要添加数据!</div>
      </template>
      <!-- 排序列 -->
      <template #sort="{ record }">
        <a-input-number
          :default-value="record.sort"
          mode="button"
          @change="changeSort($event, record.id)"
          :min="0"
          :max="1000"
        />
      </template>
      <!-- 状态列 -->
      <template #status="{ record }">
        <a-switch :checked-value="1" unchecked-value="2" @change="changeStatus($event, record.id)"
          :default-checked="record.status == 1" />
      </template>
    </ma-crud>
  </a-modal>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { dict } from '@/api/system/dict'
import { Message } from '@arco-design/web-vue'
import insurance from '@/api/admin/insurance'

const crudRef = ref()
const visible = ref(false)
const currentRow = ref({ id: undefined, name: undefined })
const car_id = ref(0)

const numberOperation = (record, newValue, numberName) => {
  insurance.numberOperation({ id: record.id, numberName, numberValue: newValue }).then( res => {
    if (res.success) {
      Message.success(res.message)
      record[numberName] = newValue
    }
  }).catch( e => { console.log(e) } )
}


const switchStatus = (record, statusValue, statusName) => {
  insurance.changeStatus({ id: record.id, statusName, statusValue }).then( res => {
    if (res.success) {
      Message.success(res.message)
      record[statusName] = statusValue
    }
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'insurance',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: insurance.getList,
  // recycleApi: insurance.getRecycleList,
  requestParams: { orderBy: ['id'], orderType: 'desc', car_id: '' },
  beforeAdd: (form) => {
      form.car_id = car_id.value
      return true
  },
  add: {
    show: true,
    api: insurance.save,
    auth: ['admin:insurance:save']
  },
  edit: {
    show: true,
    api: insurance.update,
    auth: ['admin:insurance:update']
  },
  delete: {
    show: true,
    api: insurance.deletes,
    auth: ['admin:insurance:delete'],
    realApi: insurance.realDeletes,
    realAuth: ['admin:insurance:realDeletes']
  },
  /*recovery: {
    show: true,
    api: insurance.recoverys,
    auth: ['admin:insurance:recovery']
  },
  import: {
    show: true,
    url: 'admin/insurance/import',
    templateUrl: 'admin/insurance/downloadTemplate',
    auth: ['admin:insurance:import']
  },
  export: {
    show: true,
    url: 'admin/insurance/export',
    auth: ['admin:insurance:export']
  }*/
})

const changeSort = async (value, id) => {
  const response = await dict.numberOperation({ id, numberName: 'sort', numberValue: value })
  if (response.success) {
    Message.success(response.message)
  }
}

const open = (row) => {
  currentRow.value = row
  options.requestParams.car_id = row.id
  car_id.value = row.id
  crudRef.value.requestData()
  visible.value = true
}

// const crud = reactive({
//   autoRequest: false,
//   api: dict.getPageList,
//   recycleApi: dict.getRecyclePageList,
//   showIndex: false,
//   pageLayout: 'fixed',
//   rowSelection: { showCheckedAll: true },
//   operationColumn: true,
//   operationColumnWidth: 160,
//   beforeAdd: (form) => {
//     form.code = currentRow.value?.code
//     form.type_id = currentRow.value?.id
//     return true
//   },
//   add: { show: true, api: dict.saveDictData, auth: ['system:dict:save'] },
//   edit: { show: true, api: dict.updateDictData, auth: ['system:dict:update'] },
//   delete: {
//     show: true,
//     api: dict.deletesDictData, auth: ['system:dict:delete'],
//     realApi: dict.realDeletesDictData, realAuth: ['system:dict:realDeletes']
//   },
//   recovery: { show: true, api: dict.recoverysDictData, auth: ['system:dict:recovery'] },
//   beforeRequest: (params) => params.type_id = currentRow.value.id
// })

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "保险等级",
    dataIndex: "level",
    formType: "select",
    search: true,
    commonRules: {
      required: true,
      message: "请输入保险等级"
    },
    multiple: false,
    dict: {
      data: [
        {
          label: "1级",
          value: "1"
        },
        {
          label: "2级",
          value: "2"
        },
        {
          label: "3级",
          value: "3"
        }
      ],
      translation: true
    }
  },
  {
    title: "名称",
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入名称"
    }
  },
  {
    title: "车损保障",
    dataIndex: "car_damage",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入车损保障"
    }
  },
  {
    title: "车损保障副描述",
    dataIndex: "car_damage_sub",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入车损保障副描述"
    }
  },
  {
    title: "三者保障",
    dataIndex: "tripartite",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入三者保障"
    }
  },
  {
    title: "乘客保障",
    dataIndex: "passenger",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入乘客保障"
    }
  },
  {
    title: "免折旧费",
    dataIndex: "free_depreciation",
    formType: "select",
    commonRules: {
      required: true,
      message: "请输入免折旧费"
    },
    dict: {
      data: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "无需垫付",
    dataIndex: "no_need_advance",
    formType: "select",
    commonRules: {
      required: true,
      message: "请输入无需垫付"
    },
    dict: {
      data: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "免停运费",
    dataIndex: "free_outage",
    formType: "select",
    commonRules: {
      required: true,
      message: "请输入免停运费"
    },
    dict: {
      data: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "原本单价(天)",
    dataIndex: "original_price",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入原本单价(天)"
    }
  },
  {
    title: "单价(天)",
    dataIndex: "price",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入单价(天)"
    }
  },
  {
    title: "详情",
    dataIndex: "detail",
    formType: "editor",
    hide: true,
    commonRules: {
      required: true,
      message: "请输入详情"
    }
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },
])

defineExpose({ open })
</script>

<style scoped>
.tips { color: red }
</style>